import { C_ScrewColorEnum } from "../../C_Config";
import C_App from "../../core/C_App";
import { C_NotificationConst } from "../../core/C_NotificationConst";
import C_Box from "./C_Box";
import C_Hole from "./C_Hole";

const { ccclass, property } = cc._decorator;
/** 螺丝箱子 */
@ccclass
export default class C_SpBox extends C_Box {



    protected onLoad(): void {
        this.node.children.forEach(v => {
            this.holes.push(v.getComponent(C_Hole));
        })
        this.total = this.holes.length;
    }

    initBox(color: number) {
        this.holes.forEach(v => {
            v.initHole(color);
        })
        this.color = color;
        this._occupy = [];
        this.pos = [];
        this.isActive = true;

    }

    checkColor(color: C_ScrewColorEnum) {
        return true;
        // for (let i = 0; i < this.holes.length; i++) {
        //     const hole = this.holes[i];
        //     if (hole.color == color && !this.pos[i] && !this._occupy[i]) {
        //         return true;
        //     }
        // }
        // return false;
    }

    getScrewNum() {
        let num = 0
        for (const value of this.pos) {
            if (value) {
                num++;
            }
        }
        for (const value of this._occupy) {
            if (value) {
                num++;

            }
        }
        return num;
    }

    getHoleByColor(color: number) {
        if (this.isActive) {
            for (let index = 0; index < this.holes.length; index++) {
                const hole = this.holes[index];
                if (hole.color == color && (this.pos[index])) {
                    return { hole, index };
                }
            }
        }
    }

    addScrew(pos: number, color?: C_ScrewColorEnum) {
        // const pos = this.getEmptyPos();
        // if (pos > -1) {
        this.removeOccupy(pos);
        this.holes[pos].addScrew(this.targetColor === -1 ? color : this.targetColor);
        const result = this.checkIsFull(true);
        if (result)
            C_App.NotificationCenter.dispatch(C_NotificationConst.FULL_BOX, this);
        // if (this.targetColor !== -1) {
        //     const result = this.checkIsFull();
        //     if (result) {
        //         const x = this.node.x;
        //         cc.tween(this.node)
        //             .to(.05, { x: x + 4 })
        //             .to(.1, { x: x - 8 })
        //             .to(.05, { x: x + 4 })
        //             .union()
        //             .repeat(3)
        //             .to(.5, { y: 300 })
        //             .call(() => {
        //                 this.isActive = false;
        //                 App.NotificationCenter.dispatch(NotificationConst.FULL_BOX, this);
        //             })
        //             .start();

        //     }
        // }
        //     return true;
        // }
        // else {
        //     return false;
        // }
    }
}
